package Model.DAO.uselessDao;

import Model.DAO.uselessDao.BookDao;
import SqlGenerator.SqlFactory;

import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TestDao {
    private static InitialContext context = null;
    private DataSource dataSource = null;
    public TestDao()
    {
        try
        {
            if (context == null)
            {
                context = new InitialContext();
            }
            dataSource = (DataSource)context.lookup("java:comp/env/jdbc/mysql");
            /*
             * 进行连接测试，若无表，进行创建
             * */
            //收集实体目录，关系目录所有类
            Connection conn = dataSource.getConnection();
            Class<?> [] claz = filesmerge(new File(Class.forName("Model.Entity.User").getResource("").getPath()).listFiles(),
                    new File(Class.forName("Model.Relation.Cart").getResource("").getPath()).listFiles(),conn) ;
            //将类名单丢进sql工厂进行生成sql语句
            String sql = null;
            if(claz!=null&&claz.length!=0){
                sql = new SqlFactory().TableCreate(claz);
                //执行sql语句
                System.out.println(sql);
                if(conn.createStatement().executeLargeUpdate(sql)!=0)
                    System.out.println("更新表成功");
            }else
                System.out.println("没有任何表更新");
            conn.close();
        } catch (Exception ex) {

            Logger.getLogger(BookDao.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
    //传入文件列表，筛选出没有的表名，加入到传出类数组
    private Class[] filesmerge(File[] fia,File[] fib,Connection connection){

        File[] files = new File[fia.length+fib.length];
        System.arraycopy(fia,0,files,0,fia.length);
        System.arraycopy(fib,0,files, fia.length,fib.length);
        ArrayList<Class> target = new ArrayList();
        try {
            for(File file:fia){
                String tablename = file.getName().split("\\.")[0];
                ResultSet rs = connection.prepareStatement("select t.table_name from information_schema.TABLES t where t.TABLE_SCHEMA =\"world\" and t.TABLE_NAME='"+tablename.toLowerCase()+"';").executeQuery();
                if(!rs.next()){
                    target.add(Class.forName("Model.Entity."+tablename));
                }
            }
            for(File file:fib){
                String tablename = file.getName().split("\\.")[0];
                ResultSet rs = connection.prepareStatement("select t.table_name from information_schema.TABLES t where t.TABLE_SCHEMA =\"world\" and t.TABLE_NAME='"+tablename.toLowerCase()+"';").executeQuery();
                if(!rs.next()){
                    target.add(Class.forName("Model.Relation."+tablename));
                }
            }
            return target.toArray(new Class[target.size()]);
        }
        catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
